//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System;
    using System.Collections.ObjectModel;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Practices.Unity;

    /// <summary>
    /// Presentation logic for the DataReferenceProviderLeftPanePresenter.
    /// </summary>
    public class DataReferenceProviderLeftPanePresenter : RegistryManagerLeftPaneItemPresenterBase
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="container">IUnityContainer</param>
        /// <param name="leftPane">DataReferenceProviderLeftPane</param>
        /// <param name="service">IRegistryManagerService</param>
        public DataReferenceProviderLeftPanePresenter(
            IUnityContainer container,
            DataReferenceProviderLeftPane leftPane,
            IRegistryManagerService service)
            : base(container, service)
        {
            this.LeftPaneView = leftPane;
            leftPane.DataContext = this;
        }

        /// <summary>
        /// Gets or sets the root model.
        /// </summary>
        /// <value>The root model.</value>
        public override BaseRegMgrModel RootModel
        {
            get;
            set;
        }

        /// <summary>
        /// Intialise Left Pane Tree.
        /// </summary>
        public override void Initialize()
        {
            try
            {
                this.LeftPaneTreeItems.Clear();

                // Create the root model.
                this.RootModel = new DataReferenceProviderModel(ManagementStudioResourceManager.GetString("DataReferenceProviderLeftPaneTitle"));
                this.RootModel.Id = Guid.NewGuid();


                // Get all providers from registry.
                Collection<DataReferenceProviderModel> providers = this.RegistryManagerService.GetAllDataReferenceProviders();
                foreach (DataReferenceProviderModel provider in providers)
                {
                    this.RootModel.Children.Add(provider);
                    provider.Parent = this.RootModel;
                }

                this.LeftPaneTreeItems.Add(this.RootModel);
            }
            catch (TridentCustomException ex)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(ex.CustomMessage.ToString());
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleKnownExceptionsInUI(ex);
            }
        }
    }
}
